package com.activenetwork.custom.uielement.gson;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.List;

import com.activenetwork.util.EnvContext;
import com.google.common.collect.ImmutableMap;

public class IdentifierCollection {
	private List<PageFieldIdentifier> pages;

	public List<PageFieldIdentifier> getPages() {
		return pages;
	}

	public void setPages(List<PageFieldIdentifier> pages) {
		this.pages = pages;
	}

	public void updateXpathLookup(){
		List<PageFieldIdentifier> pageIdens = checkNotNull(this.pages);
		
		synchronized(this){
			if(EnvContext.xpathLookup == null){
				// whole view
				ImmutableMap.Builder<String, ImmutableMap<String, ImmutableMap<String,String>>> pagesBuilder = 
					new ImmutableMap.Builder<String, ImmutableMap<String, ImmutableMap<String,String>>>();
				
				for(PageFieldIdentifier pageFieldIden : pageIdens){
					// page name
					String pageName = pageFieldIden.getPageName();
					// each field on page
					List<PageField> pageFields = pageFieldIden.getPageFields();
					// Create page field map, each field has several identifier
					ImmutableMap.Builder<String, ImmutableMap<String,String>> pageFieldBuilder = new ImmutableMap.Builder<String, ImmutableMap<String,String>>();
					for(PageField field : pageFields){
						ImmutableMap<String, String> idenMap = ImmutableMap.of("xpath", field.getXpath(),"css",field.getCss());
						pageFieldBuilder.put(field.getName(),idenMap);
					}
					// put one page into whole view
					pagesBuilder.put(pageName, pageFieldBuilder.build());
				}	
				EnvContext.xpathLookup = pagesBuilder.build();
			}else{
				System.out.println("Static Xpath lookup Map has been built already");
			}
		}
		
	}
	

}
